﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class UserControl_ucPhoneBrand : System.Web.UI.UserControl
{
    TblPhoneBranDAO phoneBrandDaO = new TblPhoneBranDAO(Global.CONNECTION_STRING);
    TblModelDAO modelDAO = new TblModelDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           // this.ViewState["SelectedText"] = "All";
            LoadPhoneBrand();
            DetailBrand();
            LoadModel();
            Detail_model();
            Load_drBrand();
            string id = Request.QueryString["id"];
            string id1 = Request.QueryString["id1"];
            if (id != "")
            {
                TabContainer1.ActiveTabIndex = 0;
            }
            if (id1 != null) 
            {
                TabContainer1.ActiveTabIndex = 1;
            }
        }
    }

    private void Load_drBrand()
    {
        try
        {
            drBrandM.DataSource = phoneBrandDaO.GetAll();
            drBrandM.DataTextField = "BrandName";
            drBrandM.DataValueField = "IDBrand";
            ListItem lstItem = new ListItem("-Select-", "");
            drBrandM.Items.Insert(0, lstItem);
            drBrandM.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }
    private void LoadPhoneBrand()
    {
        grdPhoneBrand.DataSource = phoneBrandDaO.GetAll();
        grdPhoneBrand.DataBind();
    }
    private void LoadModel()
    {
        grdModel.DataSource = modelDAO.GetModel();
        grdModel.DataBind();
    }
    private bool Check_BrandExists()
    {
        bool bExists = false;
        try
        {
            bExists = phoneBrandDaO.check_phonebrand(txtPhoneBrandName.Text);
            System.Threading.Thread.Sleep(3000);
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Admin/Error.aspx", true);
        }
        return bExists;
    }

    private bool CheckModel()
    {
        bool bExists = false;
        try
        {

            bExists = modelDAO.check_model(txtModel.Text);
            System.Threading.Thread.Sleep(3000);
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Admin/Error.aspx", true);
        }
        return bExists;
    }

    public void DetailBrand()
    {
        
        string id = Request.QueryString["id"];
        if (id == null)
        {
        }
        else
        {           
           // TblPhoneBrand phoneBrand = phoneBrandDaO.GetIDBrand(Global.ConvertToInt(id));
           // txtPhoneBrandName.Text = phoneBrand.PhoneBrandName;
            DataTable dt = phoneBrandDaO.GetIDBrand(Global.ConvertToInt(id));
            foreach (DataRow dr in dt.Rows)
            {
                txtPhoneBrandName.Text = dr["BrandName"].ToString();
            }
        }
    }

    // Model

    public void Detail_model()
    {

        string id = Request.QueryString["id1"];
        if (id == null)
        {
        }
        else
        {
            // TblPhoneBrand phoneBrand = phoneBrandDaO.GetIDBrand(Global.ConvertToInt(id));
            // txtPhoneBrandName.Text = phoneBrand.PhoneBrandName;
            DataTable dt = modelDAO.GetModelById(Convert.ToInt32(id));
            foreach (DataRow dr in dt.Rows)
            {
                txtModel.Text = dr["Model"].ToString();
                //ListItem lstItem = new ListItem(dr["BrandName"].ToString(), "");
                //drBrandM.Items.Insert(0, lstItem);
                //drBrandM.SelectedIndex = -1;
                drBrandM.SelectedValue = dr["IDBrand"].ToString();

            }
        }
    }

    protected void grdPhoneBrand_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           
            HiddenField hidItemID = (HiddenField)e.Row.Cells[0].FindControl("hirID");
            hidItemID.Value = DataBinder.Eval(e.Row.DataItem, "IDBrand").ToString();
            int id = Convert.ToInt32(hidItemID.Value);

            DataTable dt = modelDAO.GetModelByIdBrand(id);

            DropDownList drModel = (DropDownList)e.Row.Cells[2].FindControl("drModel");
            // drModel.SelectedValue = dt.Rows[0][3].ToString();

            drModel.DataSource = dt;
            drModel.DataTextField = "Model";
            drModel.DataValueField = "IDModel";
            ListItem lstItem = new ListItem("-Select-", "");
            drModel.Items.Insert(0, lstItem);
            drModel.DataBind();
        }

    }
    protected void grdModel_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
    }

    protected void btnsavenew_Click1(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            TblPhoneBrand phoneBrand = new TblPhoneBrand();
            string id = Request.QueryString["id"];
            phoneBrand.PhoneBrandName = txtPhoneBrandName.Text;

            if (id == null || id == "")
            {
                if (Check_BrandExists())
                {
                    Label3.Text = " Phone Brand already existed.";
                }
                else
                {
                    phoneBrand.IDPhoneBrand = 0;
                    phoneBrandDaO.InUpPhoneBrand(phoneBrand, 1);
                }
            }
            else
            {
                int idBrand = Global.ConvertToInt(Request.QueryString["id"]);
                TblPhoneBrand phone = new TblPhoneBrand();
                DataTable phoneBrand1 = phoneBrandDaO.GetIDBrand(idBrand);              
                phone.IDPhoneBrand = idBrand;
                phone.PhoneBrandName = txtPhoneBrandName.Text;
                phoneBrandDaO.InUpPhoneBrand(phone, 2);
            }
            flag = true;
            LoadPhoneBrand();

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }

        if (flag)
        {
            LoadPhoneBrand();
        }
    }

    protected void btsaveModel_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            //TblPhoneBrand phoneBrand = new TblPhoneBrand();
            TblModel model = new TblModel();
            string id = Request.QueryString["id1"];
           // phoneBrand.PhoneBrandName = txtPhoneBrandName.Text;
            model.Model = txtModel.Text;
            model.BrandID = drBrandM.SelectedValue;

            if (id == null || id == "")
            {
                if (CheckModel())
                {
                    Label3.Text = " Model already existed.";
                }
                else
                {
                    model.IDModel = 0;
                    modelDAO.Model_InUp(model, 1);
                }
            }
            else
            {
                int idModel = Global.ConvertToInt(Request.QueryString["id1"]);
                TblModel model1 = new TblModel();
                DataTable dt = modelDAO.GetModelById(idModel);
                model1.BrandID = drBrandM.SelectedValue;
                model1.IDModel = idModel;
                model1.Model = txtModel.Text;
                modelDAO.Model_InUp(model1, 2);
            }
            flag = true;
            LoadModel();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }

        if (flag)
        {
            LoadPhoneBrand();
        }
    }


    protected void btReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/PhoneBrand.aspx");
        txtPhoneBrandName.Text = "";
        TabContainer1.TabIndex = 1;
    }
    protected void btResetModel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/PhoneBrand.aspx");
        TabContainer1.ActiveTabIndex = 1;
        txtModel.Text = "";
    }
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        // string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblPhoneBrand phoneBrand = new TblPhoneBrand();
            phoneBrand.IDPhoneBrand = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idDelete = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            DataTable dt = phoneBrandDaO.GetIDBrand(idDelete);

            phoneBrandDaO.Delete(idDelete);


            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            LoadPhoneBrand();
        }

    }

    protected void btnModelDelete_Click(object sender, ImageClickEventArgs e)
    {
        // string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
          
            TblModel model = new TblModel();
            model.IDModel = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int IDDelete = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            //DataTable dt = modelDAO.ge

            modelDAO.Delete(IDDelete);


            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            LoadModel();
        }
    }
}
